<template>
    <section>
        <!--新增界面  -->
        <el-dialog title="添加与修改" :visible.sync="dialogFormVisible">
            <el-form :model="addForm" :rules="rules" ref="addForm">
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门编号" prop="sn">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio :label="0">启用</el-radio>
                        <el-radio :label="-1">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="上级部门">
                    <el-cascader
                            v-model="addForm.path"
                            :options="options"
                            :props="{ checkStrictly: true }"
                            clearable>
                    </el-cascader>
                </el-form-item>
                <el-form-item label="上级领导" prop="managerId">
                     <el-select v-model="addForm.managerId"   placeholder="请选择">
                            <el-option  v-for="(m,index) in managers" :key="index" :label="m.username" :value="m.id">
                            </el-option>
                        </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="okForm">确 定</el-button>
            </div>
        </el-dialog>

        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <!--双向绑定的查询字段-->
                    <el-input v-model="filters.keywords" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="states" clearable  placeholder="请选择状态">
                        <el-option label="启用" value="0"></el-option>
                        <el-option label="禁用" value="-1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <!--给按钮绑定事件请求方法-->
                    <el-button type="primary" v-on:click="getDepartments">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="departments"  @selection-change="selsChange"  v-loading="listLoading" style="width: 100%;" >
            <!--显示多选框-->
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="sn" label="编号" >
            </el-table-column>
            <el-table-column prop="name" label="部门" >
            </el-table-column>
            <el-table-column prop="state" label="状态">
                <template scope="scope">
                    <span style="color: green" v-if="scope.row.state==0">启用</span>
                    <span style="color: red" v-else-if="scope.row.state==-1">禁用</span>
                </template>
            </el-table-column>
            <el-table-column prop="manager.username" label="部门经理" >
            </el-table-column>
            <el-table-column prop="parent.name" label="上级部门" >
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>
    </section>
</template>
<script>
    export default {
        data() {
            return {
                condition:false,
                //分页列表
                departments: [],
                //显示加载框
                listLoading: false,
                //记录复选框选中多少行
                sels: [],
                //当前页
                currentPage: 1,
                //每页条数
                pageSize: 10,
                //总条数
                total: 0,
                filters:{},
                states:'',
                dialogFormVisible: false,//新增界面是否显示
                addLoading: false,
                //校验规则
                rules: {
                    name: [
                        {required: true, message: '部门名称必填', trigger: 'blur'},
                    ],
                    sn: [
                        {required: true, message: '部门编号必填', trigger: 'blur'},
                    ],
                    managerId: [
                        {required: true, message: '上级部门必选', trigger: 'blur'},
                    ]
                },
                //新增界面数据初始化
                addForm: {
                    id :'',
                    name: '',
                    sn: 1,
                    state: 0,
                    path: '',
                    managerId:''
                },
                options:[],//用于显示上级部门的信息（下拉）
                managers :[]
            };
        },
        methods:{
            okForm(index,row){
                //将manager封装成对象
                let manager = {"id":this.addForm.managerId}
                //给addForm添加属性
                this.addForm["manager"]= manager

                if (this.addForm.path){
                    //添加dirPath属性
                    this.addForm["dirPath"] = this.addForm.path.toString();
                    console.debug(this.addForm.path.toString())
                    //取path索引的最后一个值
                    let parent = {"id":this.addForm.path[this.addForm.path.length-1]}
                    //console.debug(parent)
                    //给addForm添加属性
                    this.addForm["parent"]= parent
                }else {
                    this.addForm["dirPath"] = ''
                    this.addForm["parent"]= null
                }
                this.$refs["addForm"].validate((valid) => {
                    if (valid) {
                        this.$http.put("/dept",this.addForm).then(res=>{
                            //console.debug(res)
                            //关闭弹出框
                            this.dialogFormVisible = false
                            //刷新列表
                            this.loadDepartments();
                        }).catch(()=>{
                            this.$message("取消成功")
                        })
                    }
                })
            },
            //点击添加框触发函数
            handleAdd(){
                if(this.$refs["addForm"]){
                    this.$refs["addForm"].resetFields();
                }
                this.getMultistage()
                //清空缓存
                for(var k in this.addForm){
                    this.addForm[k]=''
                }
                //给状态默认值
                this.addForm.state = 0
                this.dialogFormVisible = true
            },
            //选中复选框触发函数
            selsChange(rows){
                this.sels = rows;
            },
            //点击编辑框触发函数
            handleEdit(index,row){
                console.debug(row)
                this.getMultistage()
                //数据回显
                for(var k in this.addForm){
                    this.addForm[k]=row[k]
                }
                //特殊字段回显
                this.addForm.managerId = row.manager.id
                if(row.dirPath){
                    console.debug(row.dirPath)
                    this.addForm.path = row.dirPath.split(",")
                    console.debug(this.addForm.path)

                }
                this.dialogFormVisible = true
            },
            //点击删除按钮触发函数
            handleDel(index,row){
                //console.debug(row)
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    this.$http.delete("/dept/"+row.id).then(res=>{
                        //解构表达式
                        let {success,msg} = res.data
                        if(success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadDepartments();
                        }
                    })
                }).catch(()=>{
                    this.$message("取消删除")
                })
            },
            //批量删除触发函数
            batchRemove(index,val){
                this.$confirm('此操作将永久删除选中文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    this.$http.post("/dept/delList",this.sels).then(res=>{
                        //console.debug(sels)
                        //解构表达式
                        let {success,msg} = res.data
                        if(success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadDepartments();
                        }
                    })
                }).catch(()=>{
                    this.$message("取消删除")
                })
            },
            //页码发生改变的时候触发函数
            handleCurrentChange(val){
                //重新修改页码
                this.currentPage = val;
                //重新加载列表
                this.loadDepartments();
            },
            //高级查询
            getDepartments(){
                this.currentPage=1,
                this.loadDepartments()
            },
            //加载员工分页列表
            loadDepartments(){
                //分页传递参数
                let param = {
                    "keyWords": this.filters.keywords,
                    "states" : this.states,
                    "currentPage": this.currentPage,
                    "pageSize": this.pageSize
                };
                this.$http.post("/dept", param).then(res => {
                    //console.debug(res)
                    let {data, totals} = res.data;
                    this.departments = data;
                    this.total = totals;
                });
            },
            getMultistage(){
                this.$http.post("/dept/getMultistage").then(res=>{
                    console.debug(res)
                    this.options=res.data
                })
            }
        },
        mounted(){
            this.$http.get("/dept/find").then(res => {
                //console.debug(res.data)
                this.options=res.data
            })
            this.$http.get("/dept/getManager").then(res => {
                //console.debug(res)
                this.managers=res.data
            })
            //调用分页列表
            this.loadDepartments();
        }
    };
</script>